package com.mobile.service.impl;

import com.mobile.mapper.SetmealMapper;
import com.mobile.pojo.Setmeal;
import com.mobile.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Encounter
 * @date 2024/10/05 13:47<br/>
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService
    {
        @Autowired
        private SetmealMapper setmealMapper;
        @Autowired
        private Configuration freemarkerConfig;
        
        //静态文件存放路径
        @Value("${freemarker.out.path}")
        private String path;
        
        /**
         * 查询全部
         *
         * @return {@link List }<{@link Setmeal }>
         */
        @Override
        public String queryAll(Model model) throws Exception
            {
                String fileName = "setmeal.html";
                if (!exist(fileName))
                    {
                        Map<String, Object> map = new HashMap<>();
                        List<Setmeal> setmealList = setmealMapper.query(map);
                        model.addAttribute("setmealList", setmealList);
                        
                        Template template = freemarkerConfig.getTemplate("mobile_setmeal.ftl");
                        try
                            {
                                template.process(model, new FileWriter(path + fileName));
                            }
                        catch (Exception e)
                            {
                                log.error("生成套餐列表页面出错", e);
                                throw e;
                            }
                        
                        for (Setmeal setmeal : setmealList)
                            {
                                Integer id = setmeal.getId();
                                String fileDetailName = "setmeal_detail_" + id + ".html";
                                generateOrderDetailsPage(model, id, fileDetailName);
                            }
                    }
                //log.info("path为：{}", path);
                return path + fileName;
            }
        
        /**
         * 找到细节由id
         *
         * @param id    id
         * @param model 型
         * @return {@link String }
         * @throws Exception 异常
         */
        @Override
        public String findDetailById(Integer id, Model model) throws Exception
            {
                String fileName = "setmeal_detail_" + id + ".html";
                if (!exist(fileName))
                    {
                        generateOrderDetailsPage(model, id, fileName);
                    }
                return path + fileName;
            }
        
        /**
         * 判断文件是否存在
         *
         * @param fileName 文件名
         * @return boolean
         */
        private boolean exist(String fileName)
            {
                File file = new File(path + fileName);
                return file.exists();
            }
        
        /**
         * 生成订单详情页面
         *
         * @param model          型
         * @param id             id
         * @param fileDetailName 文件详细信息名称
         * @throws IOException       io异常
         * @throws TemplateException 模板异常
         */
        private void generateOrderDetailsPage(Model model, Integer id, String fileDetailName) throws IOException, TemplateException
            {
                Setmeal setmealDetail = setmealMapper.findDetailById(id);
                if (setmealDetail == null)
                    {
                        log.error("setmealDetail is null");
                        return;
                    }
                
                model.addAttribute("setmeal", setmealDetail);
                Template template = freemarkerConfig.getTemplate("mobile_setmeal_detail.ftl");
                try
                    {
                        template.process(model, new FileWriter(path + fileDetailName));
                    }
                catch (Exception e)
                    {
                        log.error("生成套餐详情页面出错 ID: {}", id, e);
                        throw e;
                    }
            }
    }
